package com.zfl;

public class RobIII {

    public int rob(TreeNode root) {


        int[] res = postorder(root);
        return Math.max(res[0], res[1]);
    }

    private int[] postorder(TreeNode root) {

        if (root == null) {
            return new int[]{0, 0};
        }
        //返回值[rob,notRob]

        int[] left = postorder(root.left);
        int[] right = postorder(root.right);

        int rob = left[1] + right[1]+root.val;
        int notRob = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);

        return new int[]{rob, notRob};
    }
}
